home *** CD-ROM | disk | FTP | other *** search
-
- #include "kalaha.h"
-
-
- /****************** GLOBALS *********************************/
- char *program_name = "Kalaha";
- char *iconbar_menu_entries = ">Info,New Game,>Set Up,Quit";
- char *NewGame_sub_entries = "Computer,User";
- char version_string[40] = "1.00";
-
-
-
- menu iconbar_menu, NewGame_sub;
- wimp_w main_window_handle;
- BOOL main_window_open = FALSE;
-
- /* Current board situation */
- char bot[6] = {6, 6, 6, 6, 6, 6};
- char top[6] = {6, 6, 6, 6, 6, 6};
- char bottot = 1;
- char toptot = 1;
- BOOL botgo = TRUE;
- /* set up info */
- int starterplayer = UserStarts;
- int level = 1; /* level can also signify user v user */
- BOOL soundon = TRUE;
- BOOL automove = TRUE;
- /* for displaying move */
- int nofplops = 0;
- int plopsdone = 0;
- Plop pendingplops[74];
-
-
- /******************* FUNCTIONS ******************************/
- static BOOL create_window(char *name, wimp_w *handle);
-
- static void open_window(wimp_openstr *o);
-
- static void iconbar_click(wimp_i icon);
-
- static void setupdbox(void);
-
- static void info_about_program(void);
-
- static void iconbar_menu_proc(void *handle, char *hit);
-
- static void main_window_events(wimp_eventstr *e, void *handle);
- /* the main events are dealt with by
- redraw_main_window()
- icon_hit(int i)
- */
- static void init_reinit(void);
-
- static BOOL initialise(void);
-
-
-
-
- BOOL create_window(char *name, wimp_w *handle)
- {
- wimp_wind *window;
-
- window = template_syshandle(name);
- if (window == 0)
- return FALSE;
-
- return (wimpt_complain(wimp_create_wind(window, handle)) == 0);
- }
-
-
-
- void open_window(wimp_openstr *o)
- {
- wimpt_noerr(wimp_open_wind(o));
- if ( !alarm_anypending((void *)0) )
- alarm_set(alarm_timenow()+200, alarm_handler_proc, (void *)0 );
- }
-
-
-
- void iconbar_click(wimp_i icon)
- {
- wimp_wstate state;
- icon = icon;
-
- if (!main_window_open)
- {
- wimpt_noerr(wimp_get_wind_state(main_window_handle, &state));
- state.o.behind = -1;
- open_window(&state.o);
- main_window_open = TRUE;
- }
- }
-
-
-
- void setupdbox(void)
- {
- dbox d;
- BOOL filling = TRUE;
- int i;
- wimp_w dbhandle;
- int i_hit[20]; /* safety net */
- wimp_which_block iblk;
- int emask;
- wimp_redrawstr r;
-
-
- d = dbox_new("SetUpdbox");
- if (d == NULL)
- werr(TRUE, "Fatal internal error ( %d )", __LINE__);
-
- /* fill in fields, set icon select states */
- dbhandle = dbox_syshandle(d);
-
- for (i=SetUpdbox_L0; i<=SetUpdbox_UU; i++)
- {
- if (i==level+SetUpdbox_L0)
- set_icon_select_state(dbhandle, i, TRUE);
- else
- set_icon_select_state(dbhandle, i, FALSE);
- }
- dbox_setnumeric(d, SetUpdbox_Sound, soundon);
- dbox_setnumeric(d, SetUpdbox_Auto, automove);
-
- /* ready for wimp_which_icon() */
- iblk.window = dbhandle;
- iblk.bit_mask = icon_ESGmask | wimp_ISELECTED;
-
- /* open dbox */
- dbox_show(d);
- emask = event_getmask();
- event_setmask(0); /* dbox seems to need this when alarm's on. */
- while (filling)
- {
- switch (dbox_fillin(d))
- {
- case 0: /* hit on OK - gather all info */
- iblk.bit_set = (2*icon_ESGlow) | wimp_ISELECTED;
- wimpt_noerr(wimp_which_icon(&iblk, i_hit));
- switch (*i_hit)
- {
- case SetUpdbox_L0:
- case SetUpdbox_L1:
- case SetUpdbox_L2:
- case SetUpdbox_L3:
- level = (*i_hit) - SetUpdbox_L0;
- starterplayer = UserStarts;
- menu_setflags(NewGame_sub, NewGame_sub_Comp, 0, 0);
- break;
-
- case SetUpdbox_UU:
- level = (*i_hit) - SetUpdbox_L0;
- starterplayer = UservUser;
- menu_setflags(NewGame_sub, NewGame_sub_Comp, 0, 1);
- break;
- }
- soundon = dbox_getnumeric(d, SetUpdbox_Sound);
- automove = dbox_getnumeric(d, SetUpdbox_Auto);
- filling = dbox_persist();
- r.w = main_window_handle; /* redraw arrow to reflect automove */
- find_hollow_box(&(r.box), 14);
- wimp_force_redraw(&r);
- break;
-
- case dbox_CLOSE:
- filling = FALSE;
- break;
-
- default:
- break;
- }
- }
- dbox_dispose(&d);
- event_setmask(emask);
- }
-
-
-
-
- void info_about_program(void)
- {
- dbox d;
-
- if (d = dbox_new("info_dbox"), d != NULL)
- {
- dbox_setfield(d, info_dbox_version_field, version_string);
- dbox_show(d);
- dbox_fillin(d);
- dbox_dispose(&d);
- }
- }
-
-
-
- void iconbar_menu_proc(void *handle, char *hit)
- {
- handle = handle;
-
- switch (hit[0])
- {
- case iconbar_menu_Info:
- info_about_program();
- break;
-
- case iconbar_menu_NewGame:
- switch (hit[1])
- {
- case 0: /* No submenu hit. ANSI C manual p343 says
- hits terminated by -1 but really 0 */
- if (starterplayer == ComputerStarts)
- starterplayer = UserStarts;
- else if (starterplayer == UserStarts)
- starterplayer = ComputerStarts;
- break;
-
- case NewGame_sub_Comp:
- starterplayer = ComputerStarts;
- break;
-
- case NewGame_sub_User:
- starterplayer = UserStarts;
- break;
- }
- init_reinit();
- iconbar_click((wimp_i)0);
- break;
-
- case iconbar_menu_SetUp:
- setupdbox();
- break;
-
- case iconbar_menu_Quit:
- exit(0);
- break;
- }
- }
-
-
-
- void main_window_events(wimp_eventstr *e, void *handle)
- {
- handle = handle;
-
- switch (e->e)
- {
- case wimp_EREDRAW:
- redraw_main_window(e->data.o.w);
- break;
-
- case wimp_EOPEN:
- open_window(&e->data.o);
- break;
-
- case wimp_ECLOSE:
- alarm_removeall((void*)0);
- wimpt_noerr(wimp_close_wind(e->data.o.w));
- main_window_open = FALSE;
- break;
-
- case wimp_EBUT:
- if (e->data.but.m.bbits == wimp_BLEFT)
- icon_hit(e->data.but.m.i);
- break;
-
- default:
- break;
- }
- }
-
-
-
- void init_reinit(void)
- {
- int p;
-
- srand( (unsigned int)alarm_timenow() );
-
- alarm_removeall((void*)0);
- nofplops = plopsdone = 0;
-
- for (p=0; p<6; p++)
- {
- bot[p] = 6;
- top[p] = 6;
- toptot = bottot = 1;
- }
- if (starterplayer==ComputerStarts)
- botgo = FALSE;
- else
- botgo = TRUE;
-
- for (p=0; p<13; p++)
- {
- if (p < 6)
- write_iconnum(bot[p], p);
- if (p >= 7)
- write_iconnum(top[p-7], p);
- set_icon_select_state(main_window_handle, p, FALSE);
- }
- write_iconnum(bottot, 13);
- write_iconnum(toptot, 14);
-
- wimpt_noerr(wimp_close_wind(main_window_handle));
- main_window_open = FALSE;
- }
-
-
- BOOL initialise(void)
- {
- wimpt_init(program_name);
- res_init(program_name);
- resspr_init();
- template_init();
- dbox_init();
- alarm_init();
-
- if (!create_window("main_window", &main_window_handle))
- return FALSE;
- win_register_event_handler(main_window_handle, main_window_events, 0);
-
- baricon("iconbaricon", (int)resspr_area(), iconbar_click);
-
- iconbar_menu = menu_new(program_name, iconbar_menu_entries);
- if (iconbar_menu == NULL)
- return FALSE;
- if (!event_attachmenu(win_ICONBAR, iconbar_menu, iconbar_menu_proc, 0))
- return FALSE;
- if (!event_attachmenu(main_window_handle, iconbar_menu, iconbar_menu_proc, 0))
- return FALSE;
- NewGame_sub = menu_new("Who Starts", NewGame_sub_entries);
- if (NewGame_sub == NULL)
- return FALSE;
- menu_submenu(iconbar_menu, iconbar_menu_NewGame, NewGame_sub);
-
- sprintf(version_string +strlen(version_string), " (%s)", __DATE__);
- return TRUE;
- }
-
-
-
- int main(void)
- {
- if (initialise())
- {
- init_reinit();
- while (TRUE)
- event_process();
- }
- exit(0);
- }
-
-